// 通用API页面混入
import ApiService from '@/services/api'

export default {
  data() {
    return {
      pageContent: {
        title: '加载中...',
        content: '正在加载内容...'
      },
      contactInfo: {},
      knowledgeArticles: [],
      loading: true,
      error: null
    }
  },
  
  async mounted() {
    await this.fetchAllData()
  },
  
  methods: {
    async fetchAllData() {
      await Promise.all([
        this.fetchPageContent(),
        this.fetchContactInfo(),
        this.fetchKnowledgeArticles()
      ])
    },
    
    async fetchPageContent() {
      if (!this.pageKey) return
      
      try {
        const response = await ApiService.get(`/page-content/${this.pageKey}`)
        if (response.success) {
          this.pageContent = response.data
        }
      } catch (error) {
        console.error('获取页面内容失败:', error)
        this.error = '加载页面内容失败'
      } finally {
        this.loading = false
      }
    },
    
    async fetchContactInfo() {
      try {
        const response = await ApiService.get('/contact-info')
        if (response.success) {
          this.contactInfo = response.data
        }
      } catch (error) {
        console.error('获取联系信息失败:', error)
      }
    },
    
    async fetchKnowledgeArticles() {
      try {
        const response = await ApiService.get('/knowledge-articles?pageSize=5')
        if (response.success) {
          this.knowledgeArticles = response.data
        }
      } catch (error) {
        console.error('获取知识文章失败:', error)
      }
    },
    
    formatDate(dateString) {
      const date = new Date(dateString)
      return `${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`
    },
    
    performSearch() {
      if (this.searchKeyword && this.searchKeyword.trim()) {
        this.$router.push({ 
          path: '/pet-column', 
          query: { search: this.searchKeyword } 
        })
      }
    }
  }
}
